package br.com.guiav6.sisguia.component;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import br.com.guiav6.sisguia.entity.Usuario;
import br.com.guiav6.sisguia.service.LoginService;

@Component("loginAction")
@Scope(value = "session")
public class LoginAction extends Action {
	
	String userName;
	
	String password;
	
	Usuario usuario = new Usuario();
	
	@Autowired(required = true)
	LoginService loginService;
	
	public String logarAdmin(){
		if(StringUtils.isNotBlank(userName) && StringUtils.isNotBlank(password)){
			usuario = loginService.verificarLogin(userName, password);
			if(usuario == null){
				addErrorMessage(null, "login.invalido");
				return navigate("admin.login");
			}else{
	            setSessionParam(Constants.USUARIO_SESSAO, usuario);
				return navigate("admin.home");
			}
		}else{
			addWarnMessage(null, "todos.campos.obrigatorios");
			return navigate("admin.login");
		}
	}
	
	public String logOutAdmin(){
		removeSessionParam(Constants.USUARIO_SESSAO);
		return navigate("home");
	}

	public Usuario getUsuario() {
		return usuario;
	}

	public void setUsuario(Usuario usuario) {
		this.usuario = usuario;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

}